package com.esm.sm800.sm800floating;

import com.esm.sm800.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

//语言设置
public class settings extends Activity implements View.OnClickListener {
    private TextView button_return; // 返回
    private TextView button_upload; // 数据上传

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置Activity为全屏
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_settings);

        // 返回
        button_return = (TextView) findViewById(R.id.setting_button_return);
        findViewById(R.id.setting_button_return).setOnClickListener(this);

        // 获取当前的语言设置
        String currentLanguage = GlobalUtil.getCurrentLanguage(this);
        // 根据语言设置设置对应的radiobutton为选中状态
        RadioButton rbChinese = findViewById(R.id.chinese_radio_button);
        RadioButton rbEnglish = findViewById(R.id.english_radio_button);
        RadioButton rbvietnamese = findViewById(R.id.vietnamese_radio_button);
        if (currentLanguage.equals("zh")) {
            rbChinese.setChecked(true);
        } else if (currentLanguage.equals("vi")) {
            rbvietnamese.setChecked(true);
        } else {
            rbEnglish.setChecked(true);
        }
        RadioGroup languageRadioGroup = findViewById(R.id.language_radio_group);
        languageRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.chinese_radio_button:
                    // 切换到中文界面
                    LocaleHelper.setLocale(getApplicationContext(), "zh", "CN");
                    // 重新启动整个应用
                    restartApp();
                    break;
                case R.id.english_radio_button:
                    // 切换到英文界面
                    LocaleHelper.setLocale(getApplicationContext(), "en", "US");
                    // 重新启动整个应用
                    restartApp();
                    break;
                case R.id.vietnamese_radio_button:
                    // 切换到越南文界面
                    LocaleHelper.setLocale(getApplicationContext(), "vi", ""); // 第二个参数是语言代码，第三个参数是地区代码（如果有的话）
                    // 重新启动整个应用
                    restartApp();
                    break;
            }
        });
    }

    private void restartApp() {
        // 结束当前Activity及其所有父Activity
//        finishAffinity();
//
//        // 启动主Activity
//        Intent intent = new Intent(this, MainActivity.class);
//        startActivity(intent);
//
//        // 设置新任务标志，以便从任务栈中清除其他Activity
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        startActivity(intent);
        setResult(666);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_button_return:
                this.finish();
                break;
        }
    }
}